package com.datatest.riskcheck.tools;



import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @Author lxt
 * Created by Jccfc on 2021/10/13.
 */
public class FileUtils {
    /**
     *
     * @param in xml字节流
     * @param xpath 解析Xml路径
     * @return
     */
    public static String[] getByXpathReadXml(InputStream in, String xpath){
        Set<String> contentSet = new TreeSet<>();
        try {
            SAXReader reader = new SAXReader();
            //解析xml形式的文本，得到document对象
            Document document = reader.read(in);
            List<Attribute> list = document.selectNodes(xpath);
            for (Attribute attribute : list) {
                if (!attribute.getValue().equals("undefined")){
                    contentSet.add(attribute.getValue());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return contentSet.toArray(new String[]{});
    }

    public static void main(String[] args) {
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("D:\\xml文件.xml"));
            //获的根节点
            Element rootElement = document.getRootElement();
           /* //提取名为BPMNPlane的 所有节点
            for (Iterator iterator = rootElement.elements("BPMNPlane").iterator();iterator.hasNext();){
                Element element = (Element) iterator.next();
                //System.out.println("上级节点："+element.getName());
                //提取名为BPMNShape的 所有节点
                List bpmnShape = element.elements("BPMNShape");
                for (Iterator iterator1 = bpmnShape.iterator();iterator1.hasNext();){
                    Element element1 = (Element) iterator1.next();
                    //System.out.println("上级节点："+element1.getName());
                    //遍历BPMNShape节点里的属性
                    for (Iterator iterator2 = element1.attributeIterator();iterator2.hasNext();){
                        Attribute attribute =(Attribute) iterator2.next();
                        //System.out.println("最里层节点属性名："+attribute.getName()+","+"属性值:"+attribute.getValue());
                        if ("content".equals(attribute.getName()) && !attribute.getValue().equals("undefined")){
                            System.out.println(attribute.getValue());
                        }
                    }
                }
            }*/

            /*List<Attribute> list = rootElement.selectNodes("//@content");
            for (Attribute attribute : list){
                System.out.println(attribute.getValue());
            }*/

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
